package com.jwgf.smarowindingmachine_unity.ui;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale;

import com.jwgf.smarowindingmachine_unity.R;
import com.jwgf.sdcard.SDCardManager;


import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class SystemLanguageDialog extends Dialog implements
		android.view.View.OnClickListener {
	private static final String TAG = "SystemLanguageDialog";
	Context mCtx;
	Button btn_cancel;
	Button btn_left_cancel;
	Button btn_right_ok;
	Locale locale;//

	public SystemLanguageDialog(Context context, Locale tlocale) {
		super(context, R.style.Transparent);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.system_language_warning);
		mCtx = context;
		btn_left_cancel = (Button) findViewById(R.id.btn_left_cancel);
		btn_left_cancel.setOnClickListener(this); 
		btn_right_ok = (Button) findViewById(R.id.btn_right_ok);
		btn_right_ok.setOnClickListener(this);
		
		locale = tlocale;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();

		switch (id) {
		case R.id.btn_left_cancel:
			this.dismiss();
			break;
		case R.id.btn_right_ok:
			changeLanguage(locale);
			this.dismiss();
			SDCardManager.getInstance().reboot();
			break;
		default:
			break;
		}
	}
	
	private void changeLanguage(Locale locale) {
////		try {
//			Resources resources = mCtx.getResources();
//			Configuration config = resources.getConfiguration();
//			DisplayMetrics dm = resources.getDisplayMetrics();
//			config.locale = locale;
//			resources.updateConfiguration(config, dm);
//			// Trigger the dirty bit for the Settings Provider.
////			 BackupManager.dataChanged("com.android.providers.settings");
////		} catch (RemoteException e) {
////			Log.e(TAG, e.toString());
////		}
//		Locale locale = new Locale(targetLocaleAsString);  
		  
		Class amnClass = null ;
		try {
			
			amnClass = Class.forName("android.app.ActivityManagerNative");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}  
		  Object amn = null;  
		  Configuration config = null;  
		  
		  // amn = ActivityManagerNative.getDefault();  
		  Method methodGetDefault = null;
		try {
			methodGetDefault = amnClass.getMethod("getDefault");
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}  
		  methodGetDefault.setAccessible(true);  
		  try {
			amn = methodGetDefault.invoke(amnClass);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}  
		  
		  // config = amn.getConfiguration();  
		  Method methodGetConfiguration = null;
		try {
			methodGetConfiguration = amnClass.getMethod("getConfiguration");
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}  
		  methodGetConfiguration.setAccessible(true);  
		  try {
			config = (Configuration) methodGetConfiguration.invoke(amn);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}  
		  
		  // config.userSetLocale = true;  
		  Class configClass = config.getClass();  
		  Field f = null;
		try {
			f = configClass.getField("userSetLocale");
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}  
		  try {
			f.setBoolean(config, true);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}  
		  
		  // set the locale to the new value  
		  config.locale = locale;  
		  
		  // amn.updateConfiguration(config);  
		  Method methodUpdateConfiguration = null;
		try {
			methodUpdateConfiguration = amnClass.getMethod("updateConfiguration", Configuration.class);
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}  
		  methodUpdateConfiguration.setAccessible(true);  
		  try {
			methodUpdateConfiguration.invoke(amn, config);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

}
